package com.common.utils.trans;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author bright
 */
public class TransApi {

    /**
     * 请求api接口
     */
    private static final String TRANS_API_HOST = "http://api.fanyi.baidu.com/api/trans/vip/translate";
    /**
     * 开发者ID
     */
    private static final String APP_ID = "20230308001592366";
    /**
     * 开发者密钥
     */
    private static final String SECURITY_KEY = "Qmom509J4mgE3gXBV12b";

    /**
     * 获取翻译结果
     * 模版:{"from":"zh","to":"en","trans_result":[{"src":"\u963f\u62c9\u65af\u52a0","dst":"Alaska"}]}
     *
     * @param query
     * @param from
     * @param to
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getTransResult(String query, String from, String to) throws Exception {
        Map<String, String> params = buildParams(query, from, to);
        //返回的结果是一个JSON字符串,
        // {"error_code":"52003","error_msg":"UNAUTHORIZED USER"}
        // 如{"from":"zh","to":"en","trans_result":[{"src":"\u963f\u62c9\u65af\u52a0","dst":"Alaska"}]} 111.175.22.37
        String body = HttpGet.get(TRANS_API_HOST, params);
        JSONObject jsonObject = JSONArray.parseObject(body);
        if(jsonObject.containsKey("error_msg")){
            throw new Exception(jsonObject.get("error_msg").toString());
        }
        JSONArray trans_result = jsonObject.getJSONArray("trans_result");
        return JSONArray.parseObject(trans_result.get(0).toString()).get("dst").toString();
    }

    private static Map<String, String> buildParams(String query, String from, String to) throws Exception {
        Map<String, String> params = new HashMap<>();
        params.put("q", query);
        params.put("from", from);
        params.put("to", to);
        params.put("appid", APP_ID);
        // 随机数
        String salt = String.valueOf(System.currentTimeMillis());
        params.put("salt", salt);
        // 签名
        String src = APP_ID + query + salt + SECURITY_KEY;
        params.put("sign", MD5.md5(src));
        return params;
    }

        public static void main(String[] args) throws Exception {
            TransApi api = new TransApi();
            String query = "Alaska";
            try {
                String transResult = api.getTransResult(query, "auto", "auto");
                System.out.println(transResult);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
    }
}
